package cn.cloudreal.cbms.Dictionary.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.cloudreal.cbms.Dictionary.dao.DictionaryDao;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.vo.Dictionary;
import cn.cloudreal.cbms.vo.Purview;

/**
 * 数据字典操作Dao
 * 
 * @author Gao_xuejiao
 * @data 2012-07-11
 */
public class DictionaryDaoImpl extends SqlMapClientDaoSupport implements
        DictionaryDao

{
    public static final Log log = LogFactory.getLog(DictionaryDaoImpl.class);
   
    private static final String DIC_FIND = "dictionary.findByCondition";


    /**
     * 查询所有集合信息
     */
    public List<Dictionary> findAll(Dictionary dic)
    {
        log.debug("finding all Datadic instances");
        List<Dictionary> dicList = new ArrayList<Dictionary>();
        try
        {
            dicList = getSqlMapClientTemplate().queryForList(DIC_FIND, dic);
        }
        catch (RuntimeException re)
        {
            log.error("find all failed", re);
        }
        return dicList;
    }
    
    
    public static final String DIC_FIND_BY_PARENTID = "dictionary.findByParentId";
    /**
     * 查询所有集合信息
     */
    public List<Dictionary> findByParentId(int  parentID)
    {
    	log.debug("finding all Datadic instances");
    	List<Dictionary> dicList = new ArrayList<Dictionary>();
    	try
    	{
    		dicList = getSqlMapClientTemplate().queryForList(DIC_FIND_BY_PARENTID, parentID);
    	}
    	catch (RuntimeException re)
    	{
    		log.error("find all failed", re);
    	}
    	return dicList;
    }

    
    /******************************************贝 修改*****************************************/
    // 查询所有权限信息
    private static final String GET_DIC_All = "dictionary.findAll";

	@SuppressWarnings("unchecked")
	public List<Dictionary> getAllDictionarys() {
		    List<Dictionary> dictionarysList = null;

	        // 打印进入方法体日志
	        if (log.isInfoEnabled())
	        {
	            log.info("Enter PurviewleDaoImpl getAllPurviews() method,return PurviewList");
	        }
	        dictionarysList = new ArrayList<Dictionary>();

	        // 执行当个Purview 对象查询
	        dictionarysList = getSqlMapClientTemplate().queryForList(GET_DIC_All);

	        // 打印退出方法体日志
	        if (log.isInfoEnabled())
	        {
	            log.info("Exit PurviewleDaoImpl getAllPurviews() method,return PurviewList");
	        }

	        return dictionarysList;
	}
	
    // 新增
    private static final String DIC_ADD = "dictionary.addDic";
	
	 /**
     * 新增
     * 
     * @param transientInstance
     */
    public void save(Dictionary transientInstance)
    {
        log.debug("saving DataDic instance");
        try
        {
System.out.println("--DAO-->"+transientInstance);
            getSqlMapClientTemplate().insert(DIC_ADD, transientInstance);
            log.debug("save successful");
        }
        catch (RuntimeException re)
        {
            log.error("save failed", re);
        }
    }

    // 删除
    public static final String DIC_DEL = "dictionary.delDic";
    /**
     * TODO:改进删除数据字典
     * 删除数据字典应该是删除这个节点及其子孙节点
     * 而且删除前应该判断这个数据字典是否被其它表引用，
     * 要根据项目设置（即项目中哪些表会对数据字典进行引用，做全表扫描）
     */
    public void delete(Dictionary persistentInstance)
    {
        log.debug("deleting Datadic instance");
        try
        {
            getSqlMapClientTemplate().delete(DIC_DEL, persistentInstance.getId());
            log.debug("delete successful");
        }
        catch (RuntimeException re)
        {
            log.error("delete failed", re);
        }
    }
    
    
    public void delete(int  id)
    {
    	log.debug("deleting Datadic instance");
    	try
    	{
    		getSqlMapClientTemplate().delete(DIC_DEL, id);
    		log.debug("delete successful");
    	}
    	catch (RuntimeException re)
    	{
    		log.error("delete failed", re);
    	}
    }
    
    
    /**
     * 验证数据字典项有无正在使用
     */
	public boolean isUseDictionary(int id) {
		return false;
	}

	
	 public static final String DIC_FIND_BY_ID = "dictionary.findById";
	
	 /**
	     * 根据ID查询信息
	     */
	    public Dictionary findByID(int id)
	    {
	        log.debug("finding all Datadic instances");
	        Dictionary dictionary = null;
	        try
	        {
	        	dictionary = (Dictionary) getSqlMapClientTemplate().queryForObject(DIC_FIND_BY_ID, id);
	        }
	        catch (RuntimeException re)
	        {
	            log.error("find all failed", re);
	        }
	        return dictionary;
	    }
	   
	    
	    
	    public static final String DIC_FINDBYCLASSCODE = "dictionary.findByClassCode";
	    
	    /**
	     * 根据classcode查询信息
	     */
	    public Dictionary findByClassCode(String classCode, String id)
	    {
	    	log.debug("finding all Datadic instances");
	    	Dictionary dictionary = null;
	    	try
	    	{
	    		Map params = new HashMap();
	    		params.put("classCode", classCode);
	    		params.put("id", id);
	    		dictionary = (Dictionary) getSqlMapClientTemplate().queryForObject(DIC_FINDBYCLASSCODE, params);
	    	}
	    	catch (RuntimeException re)
	    	{
	    		log.error("find all failed", re);
	    	}
	    	return dictionary;
	    }
	    public static final String DIC_FINDBYCLASSNAME = "dictionary.findByClassName";
	    
	    /**
	     * 根据className查询信息
	     */
	    public Dictionary findByClassName(String className)
	    {
	    	log.debug("finding all Datadic instances");
	    	Dictionary dictionary = null;
	    	try
	    	{
	    		dictionary = (Dictionary) getSqlMapClientTemplate().queryForObject(DIC_FINDBYCLASSNAME, className);
	    	}
	    	catch (RuntimeException re)
	    	{
	    		log.error("find all failed", re);
	    	}
	    	return dictionary;
	    }

	    
	    // 修改
	    private static final String DIC_UPDATE = "dictionary.updateDic";   
	    /**
	     * 修改数据字典
	     * 
	     * @param transientInstance
	     */
	    public void update(Dictionary transientInstance)
	    {
	        log.debug("update... Datadic instance");
	        try
	        {
	            getSqlMapClientTemplate().update(DIC_UPDATE, transientInstance);
	            log.debug("update successful");
	        }
	        catch (RuntimeException re)
	        {
	            log.error("update failed", re);
	        }
	    }

	    public static final String DIC_FINDBYIDANDCLASSCODE = "dictionary.findByIdAndClassCode";
		public Dictionary findByIdAndClassCode(String id, String classCode) {
			log.debug("finding all Datadic instances");
	    	Dictionary dictionary = null;
	    	try
	    	{
	    		Map cond = new HashMap();
	    		cond.put("id", id);
	    		cond.put("classCode", classCode);
	    		dictionary = (Dictionary) getSqlMapClientTemplate().queryForObject(DIC_FINDBYIDANDCLASSCODE, cond);
	    	}
	    	catch (RuntimeException re)
	    	{
	    		log.error("find all failed", re);
	    	}
	    	return dictionary;
		}

		public static final String DIC_FINDDICDATALIST = "dictionary.findDicDataList";
		public List<Dictionary> findDicDataList(String classCode,
				String parentId) {
			List<Dictionary> dictionaries = new ArrayList<Dictionary>();
	    	try
	    	{
	    		Map cond = new HashMap();
	    		cond.put("classCode", classCode);
	    		cond.put("parentId", parentId);
	    		dictionaries = getSqlMapClientTemplate().queryForList(DIC_FINDDICDATALIST, cond);
	    	}
	    	catch (RuntimeException re)
	    	{
	    		log.error("find all failed", re);
	    	}
			return dictionaries;
		}

  
}
